﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Services.Exceptions;
    using DotNetNuke.Services.Mail;
    using DotNetNuke.Services.Scheduling;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.IO;
    using System.Web;
    using System.Web.Hosting;

    public class StoreOrderReport : SchedulerClient
    {
        public StoreOrderReport(ScheduleHistoryItem objScheduleHistoryItem)
        {
            this.ScheduleHistoryItem = objScheduleHistoryItem;
        }

        private void DoReport()
        {
            IEnumerator enumerator=null;
            SettingsController controller2 = new SettingsController();
            PortalController controller = new PortalController();
            string xmlData = "";
            string subject = "Orders Report";
            string path = "";
            ArrayList allPortals = controller2.GetAllPortals();
            try
            {
                enumerator = allPortals.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    PortalInfo current = (PortalInfo) enumerator.Current;
                    if ((SharedFunctions.GetStoreSetting(current.PortalID, "version", "None") != "") && (SharedFunctions.GetStoreSetting(current.PortalID, "orderreport.lastday", "None") != DateAndTime.Day(DateAndTime.Today).ToString()))
                    {
                        xmlData = this.GetReportXML(current.PortalID, SharedFunctions.GetCurrentCulture());
                        NB_Store_SettingsInfo info3 = controller2.GetSetting(current.PortalID, "ordersreport.xsl", SharedFunctions.GetMerchantCulture(current.PortalID));
                        if (info3 != null)
                        {
                            path = HostingEnvironment.MapPath(info3.SettingValue);
                            if (File.Exists(path))
                            {
                                xmlData = SharedFunctions.XSLTrans(xmlData, path);
                            }
                            else
                            {
                                xmlData = "ERROR Producing Orders Report. Unable to find xsl required. (" + info3.SettingValue + ")";
                            }
                        }
                        else
                        {
                            xmlData = "ERROR Producing Orders Report. Unable to find setting 'ordersreport.xsl'.";
                        }
                        NB_Store_SettingsInfo info2 = controller2.GetSetting(current.PortalID, "ordersreport.subject", SharedFunctions.GetMerchantCulture(current.PortalID));
                        if (info2 != null)
                        {
                            subject = HttpUtility.HtmlDecode(info2.SettingValue);
                        }
                        if (SharedFunctions.GetStoreEmail(current.PortalID) != "")
                        {
                            DotNetNuke.Services.Mail.Mail.SendMail(SharedFunctions.GetStoreEmail(current.PortalID), SharedFunctions.GetMerchantEmail(current.PortalID), "", subject, xmlData, "", "HTML", "", "", "", "");
                        }
                        SharedFunctions.SetStoreSetting(current.PortalID, "orderreport.lastday", DateAndTime.Day(DateAndTime.Today).ToString(), "None", true);
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

        public override void DoWork()
        {
            try
            {
                this.Progressing();
                this.DoReport();
                this.ScheduleHistoryItem.Succeeded = true;
                this.ScheduleHistoryItem.AddLogNote("Store Reports Completed.");
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception objException = exception1;
                this.ScheduleHistoryItem.Succeeded = false;
                this.ScheduleHistoryItem.AddLogNote("Store Reports failed." + objException.ToString());
                this.ScheduleHistoryItem.Succeeded = false;
                this.Errored(ref objException);
                DotNetNuke.Services.Exceptions.Exceptions.LogException(objException);
                ProjectData.ClearProjectError();
            }
        }

        private string getOrderXML(int PortalID, int OrderStatus)
        {
            IEnumerator enumerator=null;
            string str2 = "";
            ArrayList list = new OrderController().GetOrderList(PortalID, -1, DateAndTime.DateAdd(DateInterval.Year, -2.0, DateAndTime.Today), DateAndTime.Today, OrderStatus, "");
            if (list.Count == 0)
            {
                return (str2 + "");
            }
            try
            {
                enumerator = list.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_OrdersInfo current = (NB_Store_OrdersInfo) enumerator.Current;
                    str2 = str2 + XmlUtils.Serialize(current);
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            return str2;
        }

        private string GetReportXML(int PortalID, string Lang)
        {
            IEnumerator enumerator=null;
            OrderController controller = new OrderController();
            string str2 = "<root>";
            ArrayList orderStatusList = controller.GetOrderStatusList(Lang);
            if (orderStatusList.Count == 0)
            {
                orderStatusList = controller.GetOrderStatusList("XX");
            }
            try
            {
                enumerator = orderStatusList.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_OrderStatusInfo current = (NB_Store_OrderStatusInfo) enumerator.Current;
                    if (current.OrderStatusID != 70)
                    {
                        str2 = str2 + this.getOrderXML(PortalID, current.OrderStatusID);
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            return (str2 + "</root>");
        }
    }
}

